
package com.zjxd.business.web.common;

import com.zjxd.business.web.code.ApiResultCode;

public class ApiResult {

	/* 消息 */
	private String	message;

	/* 状态码 */
	private int		code;

	/* 数据 */
	private Object	data;

	/**
	 * 操作成功的
	 * 
	 * @param data
	 * @return
	 */
	public static ApiResult success(Object data) {

		if (null == data) { return new ApiResult(ApiResultCode.success_message, ApiResultCode.success_code); }
		return new ApiResult(data);
	}

	public static ApiResult success() {

		return new ApiResult(null);
	}

	/**
	 * 操作失败的
	 * 
	 * @param code
	 *            失败原因码
	 * @param message
	 *            失败原因信息
	 * @return
	 */
	public static ApiResult fail(int code) {

		return new ApiResult(code);
	}

	/**
	 * 操作失败
	 * 
	 * @return
	 */
	public static ApiResult fail() {

		return new ApiResult(ApiResultCode.fail_message, ApiResultCode.fail_code);
	}

	/**
	 * 失败带原因
	 * 
	 * @param message
	 * @return
	 */
	public static ApiResult fail(String message) {

		return new ApiResult(message, ApiResultCode.fail_code);
	}

	public ApiResult(int code) {

		this.code = code;
		switch (code) {
			case ApiResultCode.parameter_error_code :
				this.message = ApiResultCode.parameter_error_message;
				break;
			case ApiResultCode.fail_code :
				this.message = ApiResultCode.fail_message;
				break;
			case ApiResultCode.error_code :
				this.message = ApiResultCode.error_message;
				break;
			case ApiResultCode.success_code :
				this.message = ApiResultCode.success_message;
				break;
			default :
				this.message = "请通知我，及时更新错误文档";
				break;
		}
		this.data = ApiResultCode.no_data;
	}

	public ApiResult(String message, int code, Object data) {

		this.message = message;
		this.code = code;
		this.data = data;
	}

	public ApiResult(Object data) {

		this.code = ApiResultCode.success_code;
		this.message = ApiResultCode.success_message;
		this.data = data;
	}

	public ApiResult(String message, int code) {

		this.message = message;
		this.code = code;
		this.data = ApiResultCode.no_data;
	}

	public String getMessage() {

		return message;
	}

	public void setMessage(String message) {

		this.message = message;
	}

	public int getCode() {

		return code;
	}

	public void setCode(int code) {

		this.code = code;
	}

	public Object getData() {

		return data;
	}

	public void setData(Object data) {

		this.data = data;
	}

	@Override
	public String toString() {

		return "ApiResult [message=" + message + ", code=" + code + ", data=" + data + "]";
	}

}
